<template>
  <div class="home-container">
    <div class="home-title">欢迎使用自助终端收费系统</div>
    <div class="input-group">
      <el-input v-model.trim="grid" ref="idInput" @focus="inputOnfocus" @keyup.enter="submitMethod"></el-input>
      <p class="input-alert">请扫处方“二维码”或输入“自助缴费ID”</p>
    </div>
    <div class="function-content">
      <div class="function-group">
        <div class="function-box" v-for="item in list" :key="item">
          <div class="function-item" @click="addNumber(item)">{{item}}</div>
        </div>
        <div class="function-box">
          <div class="function-item el-icon-back" @click="delNumber()"></div>
        </div>
        <div class="function-box">
          <div class="function-item1" @click="clearNumber()">清除</div>
        </div>
      </div>
      <div class="button-group">
        <div class="button-search" @click="submitMethod">确认</div>
      </div>
    </div>
    <div class="scroll-left">
      <!-- <div class="scroll-text">滚动文字</div> -->
    </div>
    <v-back></v-back>
    <div class="back-home-times">{{backTime}}S</div>
  </div>
</template>
  
  <script>
import vBack from '@/components/mainHeader/Back.vue';
export default {
  components: { vBack },
  data() {
    return {
      list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
      grid: '',
      backTime:60,
      type:null
    };
  },
  mounted() {
    this.type = this.$route.query.type || null;
    window.localStorage.setItem('IS_CARD_READY_STATUS', 0);
    this.initPageMethod();
  },
  methods: {
    initPageMethod(){
      this.goBackHome();
      this.inputOnfocus(0);
      document.addEventListener('keypress', this.keypressMethod);
    },
    keypressMethod(e){
      if (e.keyCode === 13 || e.which === 13) {
        this.submitMethod();
      }
    },
    addNumber(val) {
      this.grid = this.grid + val;
      this.inputOnfocus(this.grid.length);
    },
    delNumber() {
      this.grid = this.grid.substring(0, this.grid.length - 1);
      this.inputOnfocus(this.grid.length);
    },
    clearNumber() {
      this.grid = '';
      this.inputOnfocus(0);
    },
    inputOnfocus(len) {
      let input = document.querySelector('.el-input__inner');
      input.focus();
      setTimeout(() => {
        input.setSelectionRange(len + 1, len + 1);
      }, 0);
    },
    submitMethod() {
      if(this.grid && this.grid.length>0 && this.$route.path=='/login' && (this.type == 1 || this.type == 2)){
          window.localStorage.setItem('IS_CARD_READY_STATUS', 1);
          console.log('this.grid',this.grid.trim());
          if (this.type == 1) {
            this.$router.push({path:'/playImte',query:{grid:this.grid.trim()}});
          } else {
            this.$router.push({path:'/query',query:{grid:this.grid.trim()}});
          }
      }
    },
    scanIsOpen() {
      this.$http.get(this.$API.SCAN_IS_OPEN).then((res) => {
        console.log('扫码器是否开启', res);
        if (res.code == 200) {
          if (res.data == true) {
            this.scanOpenMethod();
          }
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    scanOpenMethod() {
      this.$http
        .get(this.$API.SCAN_SCAN)
        .then((res) => {
          if (res.code == 200 && res.data) {
            this.grid = res.data;
            this.submitMethod();
            return false;
          }
          let status = window.localStorage.getItem('IS_CARD_READY_STATUS');
          if (status == 0) {
            this.scanOpenMethod();
          }
        })
        .catch((err) => {
          let status = window.localStorage.getItem('IS_CARD_READY_STATUS');
          if (status == 0) {
            this.scanOpenMethod();
          }
        });
    },
    scanCloseMethod() {
      this.$http.get(this.$API.SCAN_CLOSE).then((res) => {
        console.log('关闭扫码', res);
      });
    },
    goBackHome(){
      this.backTime=60;
      clearInterval(this.timerHome);
      this.timerHome=setInterval(()=>{
        if(this.backTime == 1){
          clearInterval(this.timerHome);
          this.$router.replace('/');
        }
        this.backTime--
      },1000)
    }
  },
  destroyed() {
    document.removeEventListener('keypress',this.keypressMethod)
    clearInterval(this.timerHome);
    window.localStorage.setItem('IS_CARD_READY_STATUS', 1);
  },
};
</script>
<style lang="less">
.home-container {
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #cbe5f5, #deedf6);
  overflow: hidden;
  .home-title {
    padding: 80 * @hv 80 * @hv 80 * @hv 80 * @hv;
    font-size: 76 * @hv;
    font-weight: bold;
    color: #444;
    text-align: center;
  }
  .input-group {
    padding: 15 * @hv 20%;
    .el-input__inner {
      height: 100 * @hv;
      line-height: 100 * @hv;
      font-size: 40 * @hv;
    }
  }
  .function-content {
    width: 660 * @hv;
    margin: 0 auto;
  }
  .function-group {
    display: flex;
    flex-wrap: wrap;
  }
  .function-box {
    padding: 30 * @hv;
  }
  .function-item {
    display: flex;
    width: 160 * @hv;
    height: 160 * @hv;
    border-radius: 10px;
    box-shadow: 1px 1px 4px -2px #888;
    background: #fff;
    align-items: center;
    justify-content: center;
    font-size: 80 * @hv;
    font-weight: bold;
    color: #000;
    &:active {
      background: #eee;
    }
  }
  .function-item1 {
    display: flex;
    width: 160 * @hv;
    height: 160 * @hv;
    border-radius: 10px;
    box-shadow: 1px 1px 4px -2px #888;
    background: #fff;
    align-items: center;
    justify-content: center;
    font-size: 50 * @hv;
    font-weight: bold;
    color: #fff;
    background: #f56c6c;
    &:active {
      background: #f57e7e;
    }
  }

  .button-group {
    padding: 15 * @hv;
    .button-search {
      height: 100 * @hv;
      font-size: 40 * @hv;
      background: #409eff;
      color: #fff;
      text-align: center;
      line-height: 100 * @hv;
      border-radius: 8px;
      width: 100%;
    }
  }
  .scroll-left {
    padding: 5% 15% 0 15%;
  }
  .scroll-text {
    font-size: 60 * @hv;
    color: #c02014;
  }
  .input-alert {
    padding: 4px;
    font-size: 32 * @hv;
    color: red;
  }
}
</style>